﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(UnitAnimations))]
[RequireComponent(typeof(Destructible))]
public class BaseUnit : MonoBehaviour {

    public Vector3? destination;
    public Destructible target;
    public Player owner;

    public List<Vector3> path;
    public LayerMask mask;

    public float speed;
    public float life;
    public bool canMove = true;

    public BehaviourNode behaviour;

	// Use this for initialization
	void Awake () {
        StartCoroutine(behaviour.Activate());
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (canMove && this.Alive() && destination != null)
        {
            if (path != null && path.Count > 0)
            {
                int i = Random.Range(0, path.Count);
                if (StraightLine(path[i]))
                {
                    while (path.Count > i + 1)
                        path.RemoveAt(path.Count - 1);
                }

                var dir = path[path.Count - 1] - transform.position;

                Move(dir);

                if (dir.magnitude < WorldGrid.Instance.gridSize)
                {
                    //print(path[path.Count - 1]);
                    path.RemoveAt(path.Count - 1);
                    if (path.Count == 0)
                    {
                        path = null;
                        this.PlayAnim("idle");
                        destination = null;
                    }
                }
            }
            else
            {
                if (StraightLine(destination.Value))
                {
                    var dir = destination.Value - transform.position;

                    Move(dir);

                    if (dir.magnitude < WorldGrid.Instance.gridSize)
                    {
                        this.PlayAnim("idle");
                        destination = null;
                    }
                }
                else
                {
                    path = WorldGrid.Instance.FindPath(transform.position, destination.Value, mask);
                }
            }
        }
	}

    public bool StraightLine(Vector3 position)
    {
        var dir = position - transform.position;
        return !Physics.Raycast(transform.position - dir.normalized * WorldGrid.Instance.gridSize, dir, dir.magnitude, WorldGrid.Instance.worldMask ^ mask);
    }

    void OnDrawGizmos()
    {
        if (destination != null)
        {
            Gizmos.color = Color.red;
            var dir = destination.Value - transform.position;
            var from = transform.position - dir.normalized * WorldGrid.Instance.gridSize;
            Gizmos.DrawRay(from, dir);
        }
    }

    public void Move(Vector3 direction)
    {
        direction.z = 0;
        direction.Normalize();
        this.PlayAnim("walk", direction.y < 0.01f);
        transform.Translate(direction * speed * Time.deltaTime);
    }

    public void Goto(Vector3 to)
    {
        target = null;
        path = null;
        destination = to;
    }
}
